package com.todo.biz.application.handler.bizData;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.todo.biz.application.entity.context.SimpleBusinessDataContext;
import com.todo.biz.application.remote.PlatformStrategyRemoteService;
import com.todo.common.common.entity.TodoPlatformUserModel;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.strategy.online.service.api.response.PlatformStrategyBaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author azhebuxing
 * @date 2024/5/30 11:13
 * @description
 */
@Slf4j
@Component
public class PlatformStrategyDataHandler extends AbstractBusinessDataHandle {

    @Resource
    private PlatformStrategyRemoteService platformStrategyRemoteService;


    @Override
    protected Map<Long, SimpleBusinessDataContext> doQueryBizData(List<Long> bizIdList) {
        if (CollectionUtils.isEmpty(bizIdList)) {
            return Maps.newHashMap();
        }
        BizResponse<List<PlatformStrategyBaseResponse>> bizResponse = platformStrategyRemoteService.queryPlatformStrategyList(bizIdList);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())) {
            log.warn("PlatformStrategyDataHandler#doQueryBizData 根据主键查询内容详情完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Maps.newHashMap();
        }

        Map<Long, SimpleBusinessDataContext> resultMap = Maps.newHashMap();
        for (PlatformStrategyBaseResponse platformStrategyBaseResponse : bizResponse.getData()) {
            TodoPlatformUserModel platformUserModel = TodoCommonUtils.getPlatformUserModel();
            SimpleBusinessDataContext context = new SimpleBusinessDataContext();
            context.setBizId(String.valueOf(platformStrategyBaseResponse.getStrategyId()));

            SimpleBusinessDataContext.UserInfo userInfo = new SimpleBusinessDataContext.UserInfo();
            userInfo.setUserId(String.valueOf(platformUserModel.getUserId()));
            userInfo.setUserNikeName(platformUserModel.getUserNikeName());
            userInfo.setUserAvatar(platformUserModel.getUserAvatar());
            userInfo.setLocation(platformUserModel.getLocation());
            context.setUserInfo(userInfo);
            context.setTitle(platformStrategyBaseResponse.getTitle());
            context.setMainImg(platformStrategyBaseResponse.getMainImg());
            context.setSummary(platformStrategyBaseResponse.getRecommendDesc() == null ? "" : platformStrategyBaseResponse.getRecommendDesc().substring(0, 100));
            context.setLikeCount(platformStrategyBaseResponse.getLikeCount());
            context.setCommentCount(platformStrategyBaseResponse.getCommentCount());
            context.setCollectCount(platformStrategyBaseResponse.getViewCount());
            context.setBusinessScene(BusinessSceneEnum.PLATFORM_STRATEGY);
            resultMap.put(platformStrategyBaseResponse.getStrategyId(), context);
        }
        return resultMap;
    }

    @Override
    public BusinessSceneEnum getHandleBusinessScene() {
        return BusinessSceneEnum.PLATFORM_STRATEGY;
    }
}
